package cool.tsy.emos.wx.controller;

import cn.hutool.json.JSONUtil;
import cool.tsy.emos.wx.common.util.R;
import cool.tsy.emos.wx.db.pojo.TbRole;
import cool.tsy.emos.wx.exception.EmosException;
import cool.tsy.emos.wx.form.DeleteRoleByIdForm;
import cool.tsy.emos.wx.form.InsertRoleForm;
import cool.tsy.emos.wx.form.SearchRoleOwnPermissionForm;
import cool.tsy.emos.wx.form.UpdateRolePermissionsForm;
import cool.tsy.emos.wx.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hongxuan.wang
 * @date Created in 2021/12/28 0:01
 * @description 角色模块接口
 * @mail 1443424326@qq.com
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色模块接口")
public class RoleController {

    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @PostMapping("/searchRoleOwnPermission")
    @ApiOperation("查询角色权限列表")
    @RequiresPermissions(value = {"ROOT", "ROLE:SELECT"}, logical = Logical.OR)
    public R searchRoleOwnPermission(@Valid @RequestBody SearchRoleOwnPermissionForm form) {
        ArrayList list = roleService.searchRoleOwnPermission(form.getId());
        return R.ok().put("result", list);
    }

    @GetMapping("/searchAllPermission")
    @ApiOperation("查询所有权限")
    @RequiresPermissions(value = {"ROOT", "ROLE:SELECT"}, logical = Logical.OR)
    public R searchAllPermission() {
        ArrayList list = roleService.searchAllPermission();
        return R.ok().put("result", list);
    }

    @PostMapping("/insertRole")
    @ApiOperation("添加角色")
    @RequiresPermissions(value = {"ROOT", "ROLE:INSERT"}, logical = Logical.OR)
    public R insertRole(@Valid @RequestBody InsertRoleForm form) {
        if (!JSONUtil.isJsonArray(form.getPermissions())) {
            throw new EmosException("权限不是数组格式");
        }
        TbRole role = new TbRole();
        role.setRoleName(form.getRoleName());
        role.setPermissions(form.getPermissions());
        roleService.insertRole(role);
        return R.ok().put("result", "success");
    }

    @PostMapping("/updateRolePermissions")
    @ApiOperation("修改角色")
    @RequiresPermissions(value = {"ROOT", "ROLE:UPDATE"}, logical = Logical.OR)
    public R updateRolePermissions(@Valid @RequestBody UpdateRolePermissionsForm form) {
        if (!JSONUtil.isJsonArray(form.getPermissions())) {
            throw new EmosException("权限不是数组格式");
        }
        TbRole role = new TbRole();
        role.setId(form.getId());
        role.setPermissions(form.getPermissions());
        roleService.updateRolePermissions(role);
        return R.ok().put("result", "success");
    }

    @GetMapping("/searchAllRole")
    @ApiOperation("查询所有角色数据")
    public R searchAllRole() {
        List<TbRole> list = roleService.searchAllRole();
        return R.ok().put("result", list);
    }

    @PostMapping("/deleteRoleById")
    @ApiOperation("根据ID删除角色")
    @RequiresPermissions(value = {"ROOT","ROLE:DELETE"},logical = Logical.OR)
    public R deleteRoleById(@Valid @RequestBody DeleteRoleByIdForm form){
        roleService.deleteRoleById(form.getId());
        return R.ok().put("result","success");
    }
}
